package com.djq.hhc.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.db.sql.Order;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.djq.hhc.dto.OrderDto;
import com.djq.hhc.dto.UserInfoDto;
import com.djq.hhc.holder.UserHolder;
import com.djq.hhc.model.shop.ShopGoods;
import com.djq.hhc.model.shop.ShopOrder;
import com.djq.hhc.result.Result;
import com.djq.hhc.service.ShopGoodsService;
import com.djq.hhc.service.ShopOrderService;
import com.djq.hhc.vo.ShopGoodsVo;
import com.djq.hhc.vo.ShopOrdersVo;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: dadd7
 * @Date: 2023/03/05/下午 5:02
 * @Description:
 */

@RestController
@RequestMapping(value = "/shop/shopOrder")
public class ShopOrderController {

    @Autowired
    private ShopOrderService shopOrderService;

    @Autowired
    private ShopGoodsService shopGoodsService;

    //删除
    @DeleteMapping("remove/{id}")
    public Result removeById(@PathVariable Long id) {
        boolean b = shopOrderService.removeById(id);
        return Result.ok(b);
    }

    //完成订单
    @PostMapping("submitOrder/{id}")
    public Result submit(@RequestHeader("Authorization") String token, @PathVariable Long id) {
        return shopOrderService.submitOrder(token,id);
    }

    //分页
    @PostMapping("{page}/{limit}/{status}")
    public Result index(
            @PathVariable Long page,
            @PathVariable Long limit,
            @PathVariable Integer status) {
        //创建page对象，传递当前页和每页记录数
        Page<ShopOrder> pageParam = new Page<>(page, limit);
        //封装条件
        QueryWrapper<ShopOrder> wrapper = new QueryWrapper<>();
        UserInfoDto user = UserHolder.getUser();
        if (user == null) {
            return null;
        }
        wrapper.eq("buyer_id", user.getId());
        wrapper.eq("status", status);
        //调用方法得到分页查询结果
        IPage<ShopOrder> pageModel = shopOrderService.page(pageParam, wrapper);
        List<OrderDto> orderDtos = pageModel.getRecords().stream()
                .map(item -> {
                    OrderDto orderDto = new OrderDto();
                    ShopGoods goods = shopGoodsService.getById(item.getGoodsId());
                    BeanUtil.copyProperties(goods, orderDto);
                    orderDto.setId(item.getId());
                    orderDto.setCreateTime(item.getCreateTime());
                    return orderDto;
                })
                .collect(Collectors.toList());
        IPage<OrderDto> OrderDtoPage = new Page<>();
        BeanUtil.copyProperties(pageModel, OrderDtoPage);
        OrderDtoPage.setRecords(orderDtos);
        return Result.ok(OrderDtoPage);
    }

    //分页
    @PostMapping("{page}/{limit}")
    public Result index(
            @PathVariable Long page,
            @PathVariable Long limit,
            @RequestBody(required = false) ShopOrdersVo shopOrdersVo) {
        //创建page对象，传递当前页和每页记录数
        Page<ShopOrder> pageParam = new Page<>(page, limit);
        //获取条件值
        Long id = shopOrdersVo.getId();
        Long goodsId = shopOrdersVo.getGoodsId();
        Long buyerId = shopOrdersVo.getBuyerId();
        Integer status = shopOrdersVo.getStatus();
        //封装条件
        QueryWrapper<ShopOrder> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(id)) {
            wrapper.like("id", id);
        }
        if (!StringUtils.isEmpty(goodsId)) {
            wrapper.like("goods_id", goodsId);
        }
        if (!StringUtils.isEmpty(buyerId)) {
            wrapper.eq("buyer_Id", buyerId);
        }
        if (!StringUtils.isEmpty(status)) {
            wrapper.eq("status", status);
        }
        //调用方法得到分页查询结果
        IPage<ShopOrder> pageModel = shopOrderService.page(pageParam, wrapper);
        return Result.ok(pageModel);
    }

}
